package com.hlkj.baojin.controller;

import com.hlkj.baojin.service.IFileService;
import com.hlkj.baojin.utils.bean.JsonResult;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author liu-zhong-xin
 * @createDate 2018/2/6 0006
 */
@Api(value = "/bjjk", description = "资源文件接口")
@RestController
@RequestMapping("/basic/")
public class FileUploadingController {

    private final Logger logger = LoggerFactory.getLogger(FileUploadingController.class);
    @Autowired
    private IFileService fileService;

    /**
     * 单位添加接口
     *
     * @author lzx
     **/
    @ApiOperation(value = "上传文件", tags = {"资源管理"})
    @ApiResponses(value = {@ApiResponse(code = 200, message = "成功", response = String.class)
    })
    @PostMapping("/file")
    public ResponseEntity<JsonResult> saveFileLocalhost(MultipartFile multipartFile) {
        String id = fileService.saveFileLocalhost(multipartFile);
        return ResponseEntity.ok(JsonResult.success(id));
    }


    /**
     * 多文件具体上传时间，主要是使用了MultipartHttpServletRequest和MultipartFile
     *
     * @author lzx
     **/
    @ApiOperation(value = "批量上传文件 request.getFileS('file')", tags = {"资源管理"})
    @ApiResponses(value = {@ApiResponse(code = 200, message = "成功", response = String.class)
    })
    @PostMapping("/file/s")
    public @ResponseBody
    ResponseEntity<JsonResult> saveFilesLocalhost(MultipartFile... multipartFile) {
        List<String> collect = Arrays.stream(multipartFile).map(fileService::saveFileLocalhost).collect(Collectors.toList());
        return ResponseEntity.ok(JsonResult.success(collect));
    }

    /**
     * 单位添加接口
     *
     * @author lzx
     **/
    @ApiOperation(value = "下载文件", tags = {"资源管理"})
    @ApiResponses(value = {@ApiResponse(code = 200, message = "成功", response = String.class)
    })
    @PostMapping("/file/{id}")
    public ResponseEntity<JsonResult> getFileLocalhost(@PathVariable String id) {
        return ResponseEntity.ok(JsonResult.success(fileService.getFileLocalhost(id)));
    }

}
